package cn.com.nr.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.service.GasStationinfoService;
import cn.com.nr.entity.NrDetectionEntity;
import cn.com.nr.service.NrDetectionService;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;

/**
 * 摄像头检测
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-10-15 17:31:11
 */
@Controller
@RequestMapping("/nr/detection")
public class NrDetectionController {
	@Autowired
	private NrDetectionService nrDetectionService;
	@Autowired
	private HttpSessionService httpSessionService;
	@Autowired
	private GasStationinfoService gasStationinfoService;
	@Autowired
	private DeptService deptService;
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/list")
	@ResponseBody
	public DataResult list(@RequestBody NrDetectionEntity nrDetection) throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String currentDeptId = httpSessionService.getCurrentDeptId();
		String taxorgcode = nrDetection.getTaxorgcode();
		
		String stationinfoName = nrDetection.getStationinfoName();
		List<String> stationinfoIdList = new ArrayList<>();
		if (TsBqUtils.isNotNull(stationinfoName)) {
			LambdaQueryWrapper<GasStationinfoEntity> queryStationinfo = Wrappers.lambdaQuery();
			queryStationinfo.like(GasStationinfoEntity::getName, stationinfoName);
			List<GasStationinfoEntity> list = gasStationinfoService.list(queryStationinfo);
			if (TsBqUtils.isNotNull(list)) {
				for (GasStationinfoEntity stationinfo : list) {
					String id = stationinfo.getId();
					stationinfoIdList.add(id);
				}
			} else {
				return DataResult.fail("未查询到企业！");
			}
		}

		String createTimeStart = nrDetection.getCreateTimeStart();
		String createTimeEnd = nrDetection.getCreateTimeEnd();

		Page page = new Page(nrDetection.getPage(), nrDetection.getLimit());
		LambdaQueryWrapper<NrDetectionEntity> queryWrapper = Wrappers.lambdaQuery();

		// 查询条件示例
		if (!StringUtils.isEmpty(createTimeStart)) {
			queryWrapper.ge(NrDetectionEntity::getCreateTime, sdf.parse(createTimeStart + " 00:00:00"));
		}
		if (!StringUtils.isEmpty(createTimeEnd)) {
			queryWrapper.le(NrDetectionEntity::getCreateTime, sdf.parse(createTimeEnd + " 23:59:59"));
		}

		if (!StringUtils.isEmpty(taxorgcode)) {
			queryWrapper.eq(NrDetectionEntity::getTaxorgcode, taxorgcode);

		} else {
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapper.eq(NrDetectionEntity::getTaxorgcode, currentDeptId);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				queryWrapper.like(NrDetectionEntity::getTaxorgcode, currentDeptId.substring(0, 5));
			} else {
				// 省局
			}
		}

		if (TsBqUtils.isNotNull(stationinfoIdList)) {
			queryWrapper.in(NrDetectionEntity::getStationId, stationinfoIdList);
		}

		IPage<NrDetectionEntity> iPage = nrDetectionService.page(page, queryWrapper);
		List<NrDetectionEntity> records = iPage.getRecords();
		for (NrDetectionEntity entity : records) {
			SysDept sysDept = deptService.getById(entity.getTaxorgcode());
			// taxorgName
			entity.setTaxorgName(sysDept.getName());
			GasStationinfoEntity gasStationinfoEntity = gasStationinfoService.getById(entity.getStationId());
			if(TsBqUtils.isNotNull(gasStationinfoEntity)){
				entity.setStationinfoName(gasStationinfoEntity.getName());

			}
		
		}
		
		
		return DataResult.success(iPage);
	}

}
